/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pedidos.servidor.converter;

import br.com.pedidos.servidor.dao.MesaDAO;
import br.com.pedidos.servidor.entity.Mesa;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Weslley
 */
@FacesConverter(value="mesaConverter")
public class MesaConverter implements Converter{
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Mesa mesa = null;
        MesaDAO mesaDAO = new MesaDAO();
        if (value == null || value.isEmpty()) {
            return mesa;
        } else {
            mesaDAO.startOperation();
            mesa = mesaDAO.buscaMesaPorNumero(Integer.valueOf(value));
            mesaDAO.stopOperation(false);

            return mesa;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        } else {
            if (value instanceof Mesa) {
                Mesa mesa = (Mesa) value;
                if (mesa.getNumero() != 0) {
                    return String.valueOf(mesa.getNumero());
                } else {
                    return "";
                }
            } else {
                return "";
            }
        }
    }
    
}
